package com.myl.web.controller.store;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.github.pagehelper.PageInfo;
import com.myl.domain.store.QuestionItem;
import com.myl.service.store.QuestionItemService;
import com.myl.service.store.impl.QuestionItemServiceImp;
import com.myl.utils.BeanUtil;
import com.myl.web.controller.BaseServlet;

/**
 * Servlet implementation class QuestionItemServlet
 */
@WebServlet("/store/questionItem")
public class QuestionItemServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public QuestionItemServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String operation = request.getParameter("operation");
		if("list".equals(operation)){
			
			this.list(request,response);
			
		}else if ("toAdd".equals(operation)) {
			this.toAdd(request, response);
		}else if ("save".equals(operation)) {
			this.save(request, response);
		}else if ("toEdit".equals(operation)) {
	         this.toEdit(request,response);
        }else if ("edit".equals(operation)) {
        	this.edit(request,response);
		}else if ("delete".equals(operation)) {
        	this.delete(request,response);
		}else if ("saveorUpdate".equals(operation)) {
        	this.saveorUpdate(request,response);
		}
	
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class,"yyyy-MM-dd");
		questionItemService.delete(questionItem);
		list(request, response);
		
	}

	private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class,"yyyy-MM-dd");
		System.out.println(questionItem);
		questionItemService.update(questionItem);
				
		list(request, response);
	}

	private void toEdit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

		String id = request.getParameter("id");
		QuestionItem questionItem = questionItemService.findById(id);
		request.setAttribute("questionItem", questionItem);
		request.setAttribute("operation", "edit");
		list(request, response);
		/*response.sendRedirect(request.getContextPath()+"/store/questionItem?operation=list");*/
		
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String questionId = request.getParameter("questionId");
		
		request.setAttribute("questionId", questionId);
		
		PageInfo all = questionItemService.findAll(questionId,1, 100);
		
		request.setAttribute("page", all);
		if(request.getAttribute("operation") == null){
			request.setAttribute("operation", "save");
		}
		
		request.getRequestDispatcher("/WEB-INF/pages/store/questionItem/list.jsp").forward(request, response);
		
	}

	private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		request.getRequestDispatcher("/WEB-INF/pages/store/questionItem/add.jsp").forward(request, response);
		
	}
	 private void saveorUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			
			QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class,"yyyy-MM-dd");
			
			if(StringUtils.isNotBlank(questionItem.getId())){
				System.out.println(questionItem);
				questionItemService.update(questionItem);
			}else {
				System.out.println(questionItem);
				questionItemService.save(questionItem);
			}
			list(request, response);
			
		}
	
     private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		QuestionItem questionItem = BeanUtil.fillBean(request, QuestionItem.class,"yyyy-MM-dd");
		questionItemService.save(questionItem);
		list(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
